package com.xingkeduo.dto.transter.cardlist;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xingkeduo.utils.DateUtils;
import com.xingkeduo.utils.WeekUtil;

import lombok.Data;

@Data
public class Card_list {
	private String f_id;
	private String f_card_id;
	private String f_user_id;
	private String f_wechat_id;
	private String f_store_ids;
	private String f_week_ids;
	private String f_hour_ids;
	private String f_card_type;
	private String f_least_cost;
	private String f_reduce_cost;
	private String f_discount;
	private String f_price;
	private String f_description;
	private String f_use_start_time;
	private String f_use_end_time;
	private String f_use_time;
	private String f_get_time;
	private String f_admin_id;
	private String f_createtime;
	private String f_status;
	private String f_send_type;
	private String f_from_user_id;
	private String f_type;
	private String f_fixed;
	private String f_use_limit_type;
	private String f_use_limit_start_time;
	private String f_use_limit_end_time;
	private String f_use_limit_start_times;
	private String f_use_limit_times;
	private String f_use_times;
	private String f_send_reason_type;
	private String f_remarks;
	private String store_name;
	private String use_time;
	private String use_date;
	private String f_pic;
	private String f_name;
	private String f_marketing_id;
    private String f_marketing_type;
    private Boolean need_check;
    private String cert_status;

	public void covert(JSONObject json) {
		this.f_id = json.getOrDefault("id", "")==null?"":json.getOrDefault("id", "").toString();
		this.f_card_id = json.getOrDefault("marketingId", "")==null?"":json.getOrDefault("marketingId", "").toString();
		this.f_marketing_id = json.getOrDefault("marketingId", "")==null?"":json.getOrDefault("marketingId", "").toString();
		this.f_marketing_type = json.getOrDefault("marketingType", "")==null?"":json.getOrDefault("marketingType", "").toString();
		this.f_user_id = json.getOrDefault("userId", "")==null?"":json.getOrDefault("userId", "").toString();
		this.f_wechat_id = json.getOrDefault("fromWechatId", "")==null?"":json.getOrDefault("fromWechatId", "").toString();
		JSONObject marketing = json.getJSONObject("marketingInfo");
		String works = marketing.getOrDefault("weekHourIds", "")==null?"0":marketing.getOrDefault("weekHourIds", "0").toString();
		if ("0".equals(works)) {
			this.f_week_ids = "0,1,2,3,4,5,6";
			this.f_hour_ids = "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23";
			this.use_time = "周一至周日 0-24点";
		} else {
			JSONArray array = marketing.getJSONArray("activityDays");
			if (array != null && array.size() > 0) {
				StringBuffer daysInfo = new StringBuffer();
				int size = array.size();
				Integer start = null;
				Integer end = null;
				for(int i=0;i<size;i++) {
					JSONObject o = array.getJSONObject(i);
					String daysIds = o.getOrDefault("dayids", "")==null?"":o.getOrDefault("dayids", "").toString();
					if (StringUtils.isBlank(daysIds)) {
						continue;
					}
					String startTime = o.getOrDefault("startTime", "").toString();
					int newStart = Integer.parseInt(DateUtils.getHour(DateUtils.parseDate(startTime)));
					if(start==null || start.intValue()>newStart) {
						start = newStart;
					}
					String endTime = o.getOrDefault("endTime", "").toString();
					int newEnd = Integer.parseInt(DateUtils.getHour(DateUtils.parseDate(endTime)));
					if(end==null || end.intValue()<newEnd) {
						end = newEnd;
					}
					if (daysIds.startsWith(",")) {
						daysIds = daysIds.substring(1, daysIds.length());
					}
					if (daysIds.endsWith(",")) {
						daysIds = daysIds.substring(0, daysIds.length() - 1);
					}
					daysInfo.append(WeekUtil.convertWeek(daysIds)).append(" ")
							.append(start).append("-")
							.append(end).append("点; ");
				}
				if(StringUtils.isBlank(daysInfo.toString())) {
					this.f_week_ids = "0,1,2,3,4,5,6";
					this.f_hour_ids = "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23";
					this.use_time ="周一至周日 0-24点";
				}else {
					StringBuffer hourIds = new StringBuffer();
					for (int i = start; i <= end; i++) {
						hourIds.append(i);
						if (i < end) {
							hourIds.append(",");
						}
					}
					this.f_hour_ids = hourIds.toString();
					String newWeek = works.toString().replace("7", "0");
					String[] data = newWeek.split(",");
			        Integer[] ints = new Integer[data.length];
			        //转换为int
			        for (int i=0;i<data.length;i++) {
			        //    ints[i] = StringUtil.parseInt(data[i]);
			            ints[i] = Integer.valueOf(data[i]);
			        }
			        //1.先正序排序
			        Arrays.sort(ints);
					this.f_week_ids = StringUtils.join(ints,",");
					this.use_time =daysInfo.toString();
				}
			}else {
				this.f_week_ids = "0,1,2,3,4,5,6";
				this.f_hour_ids = "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23";
				this.use_time ="周一至周日 0-24点";
			}
		}
		String storeIds = marketing.getOrDefault("storeIds", "0")==null?"0": marketing.getOrDefault("storeIds", "0").toString();
		if(StringUtils.isNotBlank(storeIds)) {
			if(storeIds.startsWith("0,")) {
				storeIds = storeIds.substring(2, storeIds.length());
			}
			if(storeIds.endsWith(",0")) {
				storeIds = storeIds.substring(0, storeIds.length()-2);
			}
		}
		this.f_store_ids = storeIds;
		this.f_card_type =json.getOrDefault("serviceType", "") ==null?"": json.getOrDefault("serviceType", "").toString();
		if ("FREE".equals(f_card_type)) {
			this.f_card_type = "DISCOUNT";
		}
		BigDecimal leastAmount = new BigDecimal(marketing.getOrDefault("leastCost", "0")==null?"0":marketing.getOrDefault("leastCost", "0").toString());
		leastAmount = leastAmount.subtract(leastAmount.setScale(1,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? leastAmount : leastAmount.setScale(1, BigDecimal.ROUND_DOWN);
		this.f_least_cost = leastAmount.toString();
		String couponAmount = json.getOrDefault("couponAmount", "")==null?"":json.getOrDefault("couponAmount", "").toString();
		if (StringUtils.isNotBlank(couponAmount)) {
			this.f_reduce_cost = new BigDecimal(couponAmount).multiply(BigDecimal.valueOf(100))
					.setScale(0, BigDecimal.ROUND_HALF_UP).toString();
			this.f_discount = f_reduce_cost;
			this.f_price = f_reduce_cost;
		} else {
			this.f_reduce_cost = "0";
			this.f_discount = "0";
			this.f_price = "0";
		}
		if ("FREE".equals(f_card_type)) {
			this.f_reduce_cost = "0";
			this.f_discount = "0";
			this.f_price = "0";
		}
		this.f_description = null;
		Date startDate = json.getDate("useStartTime");
		if (startDate != null) {
			this.f_use_start_time = (startDate.getTime() / 1000) + "";
		}
		Date endDate = json.getDate("useEndTime");
		if (endDate != null) {
			this.f_use_end_time = (endDate.getTime() / 1000) + "";
		}
		Date useDate = json.getDate("useTime");
		if (useDate != null) {
			this.f_use_time = (useDate.getTime() / 1000) + "";
		}
		Date getDate = json.getDate("createTime");
		if (getDate != null) {
			this.f_get_time = (getDate.getTime() / 1000) + "";
		}
		this.f_admin_id = null;

		Date createDate = marketing.getDate("createTime");
		if (createDate != null) {
			this.f_createtime = (createDate.getTime() / 1000) + "";
		}
		this.f_status = json.getOrDefault("status", "")==null?"":json.getOrDefault("status", "").toString();
		this.f_send_type = json.getOrDefault("sendType", "")==null?"":json.getOrDefault("sendType", "").toString();
		this.f_from_user_id = null;
		this.f_type = "1";
		this.f_fixed = f_reduce_cost;
		this.f_use_limit_type = "1";
		this.f_use_limit_start_time = null;
		this.f_use_limit_end_time = null;
		this.f_use_limit_start_times = null;
		this.f_use_limit_times = "0";
		this.f_use_times = "0";
		this.f_send_reason_type = "0";
		this.f_remarks = "0";
		//this.store_name = json.getOrDefault("storeNames", "")==null?"":json.getOrDefault("storeNames", "").toString();
		this.store_name="适用部分门店";
		if("0".equals(f_store_ids)) {
			this.store_name="适用所有门店";
		}
		this.use_date = DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT_V3);
		this.f_pic = "common";
		this.f_name = json.getOrDefault("marketingName", "")==null?"":json.getOrDefault("marketingName", "").toString();
		String auth_status = json.getOrDefault("authStatus", "0")==null?"0":json.getOrDefault("authStatus", "0").toString();
		this.need_check = Boolean.FALSE;
		this.cert_status = auth_status;
		if (StringUtils.isNotBlank(auth_status)) {
			if ("FREE".equalsIgnoreCase(f_card_type)) {
				this.need_check = Boolean.TRUE;
			} else if("DISCOUNT".equalsIgnoreCase(f_card_type) &&auth_status.equals("0")){
				this.need_check = Boolean.TRUE;
			}else {
				this.need_check = Boolean.FALSE;
			}
			if ("0".equals(auth_status)) {
				this.cert_status = "0";
			} else if ("1".equals(auth_status)) {
				this.cert_status = "1";
			} else if ("2".equals(auth_status)) {
				this.cert_status = "-1";
			} else if ("3".equals(auth_status)) {
				this.cert_status = "2";
			}
		}
	}
	
}
